﻿using System;
using Assets.Scripts.Data;
using Test30.Core;
using Test30.Engine;
using Test30.Util;
using UnityEngine;
using UnityEngine.Tilemaps;

namespace Assets.Scripts.App
{
    public class TerminalForm:MonoBehaviour
    {
        public static TerminalForm Get { get; private set; }
        public GameObject Grid { get; private set; }
        private void Awake()
        {
            Get = this;
            _floor = Resources.Load<TileBase>(Configs.GrayPath);
            _wall = Resources.Load<TileBase>(Configs.SeaPath);
            _stairUp = Resources.Load<TileBase>(Configs.StairUpPath);
            _stairDown = Resources.Load<TileBase>(Configs.StairDownPath);
        }

        public void CreateTerminal(Game game)
        {
            _game = game;
            Grid = new GameObject()
            {
                name = $"Grid"
            };
            Grid.transform.SetParent(transform);
            Grid.AddComponent<Grid>();

            GameObject map = new GameObject
            {
                name = $"Tilemap"
            };
            map.transform.SetParent(Grid.transform);

            _tileMap = map.AddComponent<Tilemap>();
            map.AddComponent<TilemapRenderer>();

        }

        
        public void _Update()
        {
            var bounds = _game.Dungeon.Bounds;
            foreach (Vec pos in bounds)
            {
                var type = _game.Dungeon.Tiles[pos].Type;

                if (type == TileType.Floor)
                    _tileMap.SetTile(new Vector3Int(pos.X, pos.Y, 0), _floor);
                if(type==TileType.Wall)
                    _tileMap.SetTile(new Vector3Int(pos.X, pos.Y, 0), _wall);
                if (type == TileType.StairsUp)
                    _tileMap.SetTile(new Vector3Int(pos.X, pos.Y, 0), _stairUp);
                if (type == TileType.StairsDown)
                    _tileMap.SetTile(new Vector3Int(pos.X, pos.Y, 0), _stairDown);
            }
        }

//        private void CreateEntities(Game game)
//        {
//            if (game.Dungeon.Entities.Count > 0)
//            {
//                foreach (Entity entity in game.Dungeon.Entities)
//                {
//                    if (entity is Monster)
//                    {
//                        Spawn(true, entity);
//                    }
//                    else if (entity is Hero)
//                    {
//                        Spawn(false, entity);
//                    }else
//                    {
//                        throw new ArgumentOutOfRangeException();
//                    }
//                }
//            }
//        }

//        private void Spawn(bool isMonster, Entity entity)
//        {
//            Vector3 pos = new Vector3(entity.Position.X, entity.Position.Y, 0);
//            if (isMonster)
//            {
//                var role = entity as Monster;
//                if (role != null)
//                {
//                    var c = (Character) role.Race.Appearance;
//                    //Monster_C(Clone)
//                    var go = ObjectPool.Get.GetObject(c.Glyph.ToString().RemoveSuffix(5));
//                    ObjectPool.Spawn(go, ObjectPool.Get.Work, pos, Quaternion.identity, 1);
//                }
//            }
//            else
//            {
//                //英雄
//            }
//        }

        private Tilemap _tileMap;
        private Game _game;
        private TileBase _floor; 
        private TileBase _wall; 
        private TileBase _stairUp; 
        private TileBase _stairDown;

    }
}
